<template>
  <div class="dict-item-box">
    <el-form
      class="filter-container"
      @submit.native.prevent
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入字典名称/字典编码/备注查询"
          clearable
          style="width: 320px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          v-hasPermi="['system:dict:query']"
          >查询</el-button
        >
        <el-button
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dict:add']"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <div class="table-box">
      <el-table
        v-loading="loading"
        :data="typeList"
        :cell-style="{ padding: '6px 0px' }"
        ref="dictTable"
        @row-click="(dict) => $emit('refreshDictItemData', dict)"
      >
        <el-table-column label="字典名称" align="center" prop="dictName" :show-overflow-tooltip="true"/>
        <el-table-column
          label="字典编码"
          align="center"
          prop="dictType"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="备注"
          align="center"
          prop="remark"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="操作"
          align="center"
          width="150px"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="small"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:dict:edit']"
              >编辑</el-button
            >
            <el-button
              size="small"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:dict:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 新增/编辑 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      @close="closeDialog"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="form.dictName" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典编码" prop="dictType">
          <el-input
            :disabled="form.dictTypeEnable"
            v-model="form.dictType"
            placeholder="请输入字典编码"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            v-model="form.remark"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" size="mini">取消</el-button>
        <el-button type="primary" size="mini" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
  refreshCache,
} from "@/api/system/dict/type";
export default {
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      //租户列表
      tenantList: [],
      // 机构树选项
      orgOptions: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: undefined,
        dictType: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictName: [
          {
            required: true,
            message: "字典名称不能为空",
            trigger: "blur",
          },
        ],
        dictType: [
          {
            required: true,
            message: "字典类型不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    const dataType = this.$route.query && this.$route.query.dataType;
    this.queryParams.dataType = dataType;
    this.getList();
  },
  methods: {
    //编辑事件
    handleUpdate(row) {
      console.log("------>> tag0-1", row);
      row.status = String(row.status);
      this.form = row;
      this.form.dictTypeEnable = true;
      this.open = true;
      this.title = "编辑";
    },
    cancel() {
      this.form = {};
      this.open = false;
      this.$refs.form.clearValidate();
    },
    submitForm() {
      if (this.form.dictId) {
        //编辑
        console.log("------>> tag1 ", this.form);
        console.log("------>> tag2--- ", this.form);
        updateType(this.form).then((response) => {
          this.open = false;
          this.getList();
        });
      } else {
        //新增
        console.log("------>> tag2 ", this.form);
        addType(this.form).then((response) => {
          this.open = false;
          this.getList();
        });
      }
    },
    closeDialog() {
      this.$refs.form.clearValidate();
    },

    //删除
    handleDelete(row) {
      this.$confirm(
        `确定进行删除操作? 删除该字典后，关联的字典项也会被删除!`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          console.log("------>> row=", row);
          this.dataListLoading = true;
          delType(row.dictId)
            .then((response) => {
              this.getList();
            })
            .catch((e) => {
              this.dataListLoading = false;
            });
        })
        .catch(() => {
          this.dataListLoading = false;
        });
    },

    getList() {
      this.dataListLoading = true;
      listType(this.queryParams)
        .then((response) => {
          this.typeList = response.rows;
          this.total = response.total;
          this.loading = false;
          if (this.typeList.length > 0) {
            // 设置第一行选中
            this.$refs.dictTable.setCurrentRow(this.typeList[0]);
            this.$emit("refreshDictItemData", this.typeList[0]);
          } else {
            // 清空选中
            this.$refs.dictTable.setCurrentRow();
            // 找不到就查询一个不存在的吧（不建议该方式），我本想清空右边的数据的
            this.$emit("refreshDictItemData", {
              dictType: "sys_user_sex",
            });
          }
        })
        .catch((e) => {
          this.dataListLoading = false;
        });
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {};
      this.form.status = "1";
      this.form.typeDictEnable = false;
      this.title = "新增";
      this.open = true;
    },
    addOrUpdateDict(dict) {
      this.dictEditFormVisible = true;
      this.$nextTick(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.dict-item-box {
  .dict-btn-list {
    padding: 16px 16px 6px;
  }

  .el-table {
    .cell {
      padding: 0 14px;
    }
  }
}
</style>
